package edu.mse.camel.client.posterboard.ui.editpart;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;

public class EditpartListener implements Adapter {

	private Notifier target;
	private GraphicalEditPart editPart;
	
	public EditpartListener(GraphicalEditPart editPart) {
		this.editPart = editPart;
	}
	
	@Override
	public Notifier getTarget() {
		return target;
	}

	@Override
	public boolean isAdapterForType(Object type) {
		return type.equals(editPart.getModel().getClass());
	}

	@Override
	public void notifyChanged(Notification notification) {
		boolean refreshChildren = false;
		if (notification.getFeature() instanceof EAttribute) {
			EAttribute attr = (EAttribute) notification.getFeature();
			if (attr.getName().equals("location") || attr.getName().equals("size")) {
				refreshChildren = true;
			}
		}
		int type = notification.getEventType();
		switch( type ) {
			case Notification.ADD:
			case Notification.ADD_MANY:
			case Notification.REMOVE:
			case Notification.REMOVE_MANY:
			case Notification.SET:
				editPart.refresh();
				if (refreshChildren) {
					for (Object child : editPart.getChildren()) {
						if (child instanceof EditPart) {
							((EditPart) child).refresh();
						}
					}
				}
			break;
		}
		
	}

	@Override
	public void setTarget(Notifier newTarget) {
		this.target = newTarget;
		
	}

}
